<?php if (!defined('THINK_PATH')) exit(); /*a:1:{s:74:"/Users/yandian/work/matian/application/admin/view/bootstrap/role/form.html";i:1492617969;}*/ ?>
<style>
    .root_nodes {
        margin: 0;
        border-bottom: 1px dashed #ccc;
        padding: 10px 0;
    }

    .ib {
        display: inline-block;
        margin-right: 10px;
    }

    .root_nodes, .sub-nodes {
        list-style: none;
    }

    .sub-nodes {
        margin-left: 20px;
        padding: 0;
    }

</style>
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>角色管理</h2>
        <ol class="breadcrumb">
            <li>
                <a href="#">首页</a>
            </li>
            <li>
                <a href="#<?php echo U('Role/'); ?>">角色管理</a>
            </li>
            <li class="active">
                <strong><?php echo !empty($isEdit)?'修改':'添加'; ?>角色</strong>
            </li>
        </ol>
    </div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <div>
                        <button class="btn btn-link" type="button" onclick="history.back()">
                            <span class="fa fa-angle-left"></span>
                            返回
                        </button>
                    </div>
                    <form method="post" class="form-horizontal" id="form1">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">角色名称</label>
                            <div class="col-sm-10">
                                <input value="<?php echo $m['title']; ?>" name="title" type="text" required class="form-control">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">权限列表</label>
                            <div class="col-sm-10" id="power-list">
                                <div class="form-control-static" style="padding-top: 0;margin-top: -3px;">
                                    <?php if(is_array($menuList)): foreach($menuList as $k1=>$m1): ?>
                                    <ul class="root_nodes">
                                        <?php if(!empty($m1['#subs'])): $m1['subs'] = $m1['#subs']; endif; ?>
                                        <li <?php if(empty($m1['subs'])): ?>class="ib"<?php endif; ?>>
                                            <label>
                                                <input <?php echo in_array($k1,$tokens)?'checked="checked"':''; ?>
                                                type="checkbox"
                                                value="<?php echo $k1; ?>">
                                                <?php echo is_array($m1)?$m1['title']:$m1; ?>
                                            </label>
                                        </li>
                                        <?php if(!empty($m1['subs'])): ?>
                                        <ul class="sub-nodes">
                                            <?php if(is_array($m1['subs'])): foreach($m1['subs'] as $k2=>$m2): if(!empty($m2['#subs'])): $m2['subs'] = $m2['#subs']; endif; ?>
                                            <li <?php if(empty($m2['subs'])): ?>class="ib"<?php endif; ?>>
                                            <label>
                                                <input <?php echo in_array($k2,$tokens)?'checked="checked"':''; ?> type="checkbox" value="<?php echo $k2; ?>">
                                                <?php echo is_array($m2)?$m2['title']:$m2; ?>
                                            </label>
                                            </li>
                                            <?php if(!empty($m2['subs'])): ?>
                                            <ul class="sub-nodes">
                                                <?php if(is_array($m2['subs'])): foreach($m2['subs'] as $k3=>$m3): ?>
                                                <li class="ib">
                                                    <label>
                                                        <input <?php echo in_array($k3,$tokens)?'checked="checked"':''; ?>
                                                        type="checkbox" value="<?php echo $k3; ?>">
                                                        <?php echo is_array($m3)?$m3['title']:$m3; ?>
                                                    </label>
                                                </li>
                                                <?php endforeach; endif; ?>
                                            </ul>
                                            <?php endif; endforeach; endif; ?>
                                        </ul>
                                        <?php endif; ?>
                                    </ul>
                                    <?php endforeach; endif; ?>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button data-style="expand-left" class="btn btn-primary ladda-button"
                                        type="button"
                                        data-op="submit">确 定
                                </button>
                            </div>
                        </div>
                        <div>
                            <button class="btn btn-link" data-op="back" type="button">
                                <span class="fa fa-angle-left"></span>
                                返回
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    (function () {
        $('#power-list').on('click', 'label', function () {
            var jq = $(this).parent();
            var cb = jq.find(':checkbox:first');
            var checked = cb[0].checked;
            var nextDiv = jq.next('ul');
            if (nextDiv.length) {
                nextDiv.find(':checkbox').each(function () {
                    this.checked = checked;
                });
            }
            var pnode = jq.parent(); //ul
            while (pnode.length) {
                if (pnode.hasClass('root_nodes')) {
                    break;
                }
                var hasChecked = false;
                pnode.find('>li').each(function () {
                    var node = $(this).find('>label>:checkbox');
                    if (node.length && node[0].checked) {
                        hasChecked = true;
                        return false;
                    }
                });
                if (!hasChecked) {
                    break;
                }
                pnode.prev().find(':checkbox').each(function () {
                    this.checked = checked || hasChecked;
                });
                pnode = pnode.parent();
            }
        });
        YForm.create('#form1', "<?php echo U('saveModel'); ?>", '<?php echo $m['id']; ?>').on('beforeSubmit', function (postData) {
            var ids = [];
            $('#power-list').find(':checked').each(function () {
                var mid = this.value;
                ids.push({
                    menu_token: mid
                });
            });
            postData.tokens = JSON.stringify(ids);
        });
    })();
</script>